package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    DishMapper dishMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    DishFlavorMapper dishFlavorMapper;

    @Override
    public Page findByLimit(Integer pageNum, Integer pageSize, String name) {
        //分页查询菜品表
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(name),Dish::getName,name);
        Page<Dish> page = new Page<>(pageNum,pageSize);
        page = dishMapper.selectPage(page,qw);
        //用菜品表中category_id,查询菜品分类表(id)的分类名
        for (Dish dish : page.getRecords()) {
            //查询分类名称  select * from category where id = 1397844263642378242
            Category category = categoryMapper.selectById(dish.getCategoryId());
            //dish的数据库表中并没有CategoryName,只在dish的bean类中定义了，只是为了给前端显示数据
            dish.setCategoryName(category.getName());

            //查询口味列表
            //用菜品表(dish)的id,去查找菜品口味表(dish_flavor)中的dish_id
            //select * from dish_flavor where dish_id = 1397850392090947585
            LambdaQueryWrapper<DishFlavor> dfqw = new LambdaQueryWrapper<>();
            //一个查询条件
            dfqw.eq(DishFlavor::getDishId,dish.getId());// where dish_id = 1397850392090947585
            //DishFlavor表示：数据库中的dish_flavor表映射到DishFlavor类中
            //getDishId:DishFlavor类中的get方法，得到数据库中dish_flavor表中的dish_id
            //dish.getId():我们上面分页查询得出的dish里的id;
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(dfqw);
            dish.setFlavors(dishFlavors);


        }
        return page;
    }

    //新增菜品
    @Override
    public void save(Dish dish) {
        dishMapper.insert(dish);

        //将菜品
        List<DishFlavor> flavors = dish.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dish.getId());
            dishFlavorMapper.insert(flavor);
        }

    }

    //回显
    @Override
    public Dish findById(Long dishId) {
        //查询基本信息
        Dish dish = dishMapper.selectById(dishId);

        //根据dish的id,查找dish_flavor的dish_id
        LambdaQueryWrapper<DishFlavor> dqw = new LambdaQueryWrapper<>();
        dqw.eq(DishFlavor::getDishId,dishId);

        List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(dqw);

        dish.setFlavors(dishFlavors);

        return dish;

    }

    @Override
    public void update(Dish dish) {
        //更改基本信息
        dishMapper.updateById(dish);

        //先删后加
        //delete from dish_flavor where dish_id = dish.getId;
        LambdaQueryWrapper<DishFlavor> dqw = new LambdaQueryWrapper<>();
        dqw.eq(DishFlavor::getDishId,dish.getId());
        dishFlavorMapper.delete(dqw);

        //获取前端新口味列表,遍历
        for (DishFlavor flavor : dish.getFlavors()) {
            //flavor.setId(dish.getId());
            flavor.setDishId(dish.getId());
            dishFlavorMapper.insert(flavor);
        }

    }

    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        //非空判断
        if (CollUtil.isNotEmpty(ids)) {
            for (Long id : ids) {
                Dish dish = new Dish();
                dish.setId(id);
                dish.setStatus(status);
                dishMapper.updateById(dish);
            }
        }
    }

    @Override
    public void deleteById(List<Long> ids) {
        //查询要删除的id的状态
        //select count(*) from dish where id = ... and status = 0;
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.in(Dish::getId,ids);
        qw.eq(Dish::getStatus,1);
        Integer dishes = dishMapper.selectCount(qw);
        //判断是否有起售商品
        if(dishes > 0){
            throw new CustomException("有起售菜品,无法删除！");
        }

        //批量删除
        dishMapper.deleteBatchIds(ids);

        //删除菜品口味 delete from dish_flavor where id in(...,...);
        LambdaQueryWrapper<DishFlavor> dqw = new LambdaQueryWrapper<>();
        dqw.in(DishFlavor::getDishId,ids);
        dishFlavorMapper.delete(dqw);

    }

    @Override
    public List<Dish> list(Long categoryId, Integer status) {
        //1.查询菜品select * from dish where categoryId = id and status =?;
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(Dish::getCategoryId,categoryId);
        qw.eq(Dish::getStatus,status);
        List<Dish> dishList = dishMapper.selectList(qw);

        for (Dish dish : dishList) {
            //select * from dish_flavor where dish_id = ?;
            LambdaQueryWrapper<DishFlavor> Dqw = new LambdaQueryWrapper<>();
            Dqw.eq(DishFlavor::getDishId,dish.getId());
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(Dqw);
            dish.setFlavors(dishFlavors);
        }
        return dishList;
    }

}
